/*
 * Moviment.cpp
 *
 *  Created on: 06/02/2013
 *      Author: eusyar
 */

#include "component/Moviment.h"
#include "util/FPS.h"
#include <iostream>

using namespace eats_core;
using namespace eats_vars;
using namespace std;

Moviment::Moviment(vector<Point> points_route, int speed, int * posX, int * posY) {
	//_route = new Route(points_route);

	_points_route = points_route;

	_speed = speed;
	_posX = posX;
	_posY = posY;
	_stopChecking = false;
}

Moviment::~Moviment() {
	// TODO Auto-generated destructor stub
}

Moviment::Moviment(int * posX, int * posY) {
	//_route = NULL;
	//_points_route = 0;
	_speed = 0;
	_posX = posX;
	_posY = posY;
	_stopChecking = false;
}

bool Moviment::setUp(){
	cout << "|INFO| Carregando a rota de movimentação" << endl;

	_route = new Route(_points_route);
	if(_route == NULL) {
		cout << "|ERRO| Não há espaço para carregar a rota" << endl;
	}

	cout << "|INFO| O espaço para rota foi separado com sucesso" << endl;
	return true;
}

bool Moviment::update(){
	if(_stopChecking ){
		return true;
	}

	Point target = _route->getTargetPosition();

	if( *_posX == target.x && *_posY == target.y){
		if( !_route->switchToNextPosition() ) {
			_stopChecking = true;
			return true;
		}
	}

	if( _speed == 0 || _route == NULL){
		return true;
	}

	if( *_posX < target.x )
		(*_posX) += _speed * FPS::FPSControl.getSpeedFactor() ;
	if( *_posX > target.x )
		(*_posX) -= _speed * FPS::FPSControl.getSpeedFactor() ;

	if( *_posY < target.y )
		(*_posY) += _speed * FPS::FPSControl.getSpeedFactor() ;
	if( *_posY > target.y )
		(*_posY) -= _speed * FPS::FPSControl.getSpeedFactor() ;


}

bool Moviment::tearDown(){
	cout << "|INFO| Liberando espaço de memória para o componente de movimentação" << endl;
	delete _route;
	return true;
}
